package com.weilian.ordering;

import java.io.File;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.weilian.ordering.common.GloableData;
import com.weilian.ordering.util.Logger;

public class ScreenSaverActivity extends Activity {
	private final static Logger log = Logger
			.getLogger(ScreenSaverActivity.class);
	private final int CHANGE_IMAGE = 0x1;

	private String imagePath = "";
	private String[] fileList = null;
	private Timer timer = null;
	private TimerTask task = null;
	// private ImageView img_screen;
	private int imagePosition = 0, imageSize = 0;
	private Bitmap bit = null;
	private HashMap<String, Bitmap> bitHash;
	private ViewFlipper viewFlipper;
	private ImageView img, img1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screensave);
		// img_screen = (ImageView)findViewById(R.id.img_screen_save);
		viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

		bitHash = new HashMap<String, Bitmap>();
		imagePosition = 0;
		imagePath = GloableData.pictureBasePath + "ads/";
		timer = new Timer();
		searchImage();
		// add onclick listener

		viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_left_in));
		viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_left_out));
		viewFlipper.setOnClickListener(onclick());
	}

	public View addImage(int imageSrc) {
		ImageView imageView = new ImageView(this);
		imageView.setBackgroundResource(imageSrc);
		return imageView;
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CHANGE_IMAGE:

				String path = (String) msg.obj;
				log.i(imagePath + path);
				if (bitHash.get(path) != null) {
					bit = bitHash.get(path);
				} else {
					bit = BitmapFactory.decodeFile(imagePath + path);
					bitHash.put(path, bit);
				}
				img = new ImageView(ScreenSaverActivity.this);
				img.setBackgroundDrawable(new BitmapDrawable(bit));
				viewFlipper.addView(img);
				viewFlipper.showNext();
				viewFlipper.removeViewAt(0);
				
				break;
			}

		};
	};

	public void searchImage() {
		File file = new File(imagePath);
		img = new ImageView(this);
		img1 = new ImageView(this);
		if (file.exists()) {
			fileList = file.list();
			log.i(fileList.length + "---");
			if (fileList.length > 0) {
				imageSize = fileList.length;
				

				bit = BitmapFactory.decodeFile(imagePath + fileList[0]);
				bitHash.put(fileList[0], bit);
				
				img.setBackgroundDrawable(new BitmapDrawable(bit));
				imagePosition++;
				if (fileList.length > 1) {
					bit = BitmapFactory.decodeFile(imagePath + fileList[1]);
					bitHash.put(fileList[1], bit);
					img1.setBackgroundDrawable(new BitmapDrawable(bit));
					imagePosition++;
				} else {
					img1.setBackgroundDrawable(new BitmapDrawable(bit));
				}
				
				viewFlipper.addView(img);
				viewFlipper.addView(img1);
				showImage();
			}else{
				img.setBackgroundResource(R.drawable.save1);
				img1.setBackgroundResource(R.drawable.save2);
				viewFlipper.addView(img);
				viewFlipper.addView(img1);
				viewFlipper.setFlipInterval(5000);
				viewFlipper.startFlipping();
			}
				

			
		}
	}

	public void showImage() {
		task = new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				log.i("in showImage task");
				if(imagePosition > imageSize-1){
					imagePosition = 0;
				}
				Message msg = new Message();
				msg.obj = fileList[imagePosition];
				msg.what = CHANGE_IMAGE;
				handler.sendMessage(msg);
				imagePosition ++;

			}
		};
		timer.schedule(task, 3000, 5000);
	}

	public OnClickListener onclick() {
		return new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.viewFlipper1:
					finish();
					break;
				}
			}
		};
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// Set<String> key = bitHash.keySet();
		// Iterator<String> ite =key.iterator();
		// while(ite.hasNext()){
		// bitHash.get(ite.next()).recycle();
		// }
		timer.cancel();
		timer = null;
		bitHash.clear();
	}
}
